﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BoatNGoat.Input.HardwareLayer;
using SharpDX.DirectInput;
using BoatNGoat.Input.ImpulseLayer;

namespace BoatNGoat.Input.ImpulseLayer
{
    public class ImpulseMapper
    {
        // Dictionaries of key binds. We will normally want to go from Impulse -> Key.
        private KeyboardController _keyboardController;
        private Dictionary<Key, string> _pressedKeyBinds;
        private Dictionary<Key, string> _heldKeyBinds;
        private Dictionary<Key, string> _releasedKeyBinds;

        public ImpulseMapper(DirectInput directInput)
        {
            _keyboardController = new KeyboardController(directInput);
            SetDefaultBinds();
        }

        public IEnumerable<string> GetImpulses()
        {
            _keyboardController.CheckInput();
            HashSet<Key> pressedKeys = _keyboardController.PressedKeys;
            HashSet<Key> heldKeys = _keyboardController.HeldKeys;
            HashSet<Key> releasedKeys = _keyboardController.ReleasedKeys;
            
            HashSet<string> impulses = new HashSet<string>();
            foreach (Key key in pressedKeys)
            {
                if (_pressedKeyBinds.ContainsKey(key))
                    impulses.Add(_pressedKeyBinds[key]);
            }

            foreach (Key key in heldKeys)
            {
                if (_heldKeyBinds.ContainsKey(key))
                    impulses.Add(_heldKeyBinds[key]);
            }

            foreach (Key key in releasedKeys)
            {
                if (_releasedKeyBinds.ContainsKey(key))
                    impulses.Add(_releasedKeyBinds[key]);
            }

            return impulses;
        }
        
        private void SetDefaultBinds()
        {
            _pressedKeyBinds = new Dictionary<Key, string>();
            _heldKeyBinds = new Dictionary<Key, string>();
            _releasedKeyBinds = new Dictionary<Key, string>();

            // Player Keybinds
            _pressedKeyBinds.Add(Key.W, "PlayerMoveUp");
            _pressedKeyBinds.Add(Key.A, "PlayerMoveLeft");
            _pressedKeyBinds.Add(Key.S, "PlayerMoveDown");
            _pressedKeyBinds.Add(Key.D, "PlayerMoveRight");
            _pressedKeyBinds.Add(Key.LeftShift, "PlayerSprintOn");

            _heldKeyBinds.Add(Key.W, "PlayerMoveUp");
            _heldKeyBinds.Add(Key.A, "PlayerMoveLeft");
            _heldKeyBinds.Add(Key.S, "PlayerMoveDown");
            _heldKeyBinds.Add(Key.D, "PlayerMoveRight");
            _heldKeyBinds.Add(Key.LeftShift, "PlayerSprintOn");

            _releasedKeyBinds.Add(Key.W, "PlayerMoveUpStop");
            _releasedKeyBinds.Add(Key.A, "PlayerMoveLeftStop");
            _releasedKeyBinds.Add(Key.S, "PlayerMoveDownStop");
            _releasedKeyBinds.Add(Key.D, "PlayerMoveRightStop");
            _releasedKeyBinds.Add(Key.LeftShift, "PlayerSprintOff");

            // Camera Keybinds
            _pressedKeyBinds.Add(Key.G, "CameraFollowNextGoat");
            _pressedKeyBinds.Add(Key.P, "CameraFollowLocalPlayer");
        }
    }
}
